package com.sgq.vue.controller.resource;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sgq.vue.entity.resource.WeatherApi;
import com.sgq.vue.enums.RequestConditionEnums;
import com.sgq.vue.result.Result;
import com.sgq.vue.service.resource.WeatherApiService;
import com.sgq.vue.utils.resource.RequestUrlBuilder;
import com.sgq.vue.utils.system.MySecurityContextHolder;
import com.sgq.vue.utils.resource.ParamType;
import com.sgq.vue.vo.query.resource.WeatherApiQueryVo;
import com.sgq.vue.vo.query.resource.WeatherApiVo;
import com.sgq.vue.vo.query.resource.WeatherRequestQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @author: 83778
 * @date: 2023-12-26 0:41
 * @desc:
 */
@RestController
@RequestMapping("/api/resource/weatherApi")
public class WeatherApiController {

    @Autowired
    private WeatherApiService weatherApiService;

    @ApiOperation("条件分页查询")
    @GetMapping("/list")
    public Result list(WeatherApiQueryVo weatherApiQueryVo){
        IPage<WeatherApi> weatherByPage = weatherApiService.findWeatherApiByPage(weatherApiQueryVo);
        return Result.ok(weatherByPage);
    }

    @ApiOperation("新增")
    @PostMapping("/add")
    public Result add(@RequestBody WeatherApi weatherApi){
        boolean save = weatherApiService.save(weatherApi);
        return Result.ok(save);
    }

    @ApiOperation("修改")
    @PutMapping("/edit")
    public Result edit(@RequestBody WeatherApi weatherApi){
        boolean update = weatherApiService.updateById(weatherApi);
        return Result.ok(update);
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id){
        boolean b = weatherApiService.removeById(id);
        return Result.ok(b);
    }

    @ApiOperation("把当前的可用状态设置成所给定的值")
    @PutMapping("/updateState/{id}/{state}")
    public Result updateState(@PathVariable Long id,@PathVariable Integer state){
        Long userId =  MySecurityContextHolder.getSysUserId();
        boolean update = weatherApiService.updateState(id,state,userId);
        return Result.ok(update);
    }

    /**
     *  展示所有构建部分固定条件的WeatherApi
     * @param weatherApiQueryVo
     * @return
     */
    @GetMapping("/build")
    public Result build(WeatherApiQueryVo weatherApiQueryVo){
        Long belongs = weatherApiQueryVo.getBelongs();
        List<WeatherApiVo> weatherRequests = new ArrayList<>();
        List<WeatherApi> weatherApis = weatherApiService.findWeatherApiByBelongs(belongs);
        weatherApis.forEach(weatherApi ->{
            //获取请求url
            String requestUrl = RequestUrlBuilder.buildRequestUrl(weatherApi);
            //获取请求名称
            String apiName = weatherApi.getApiName();
            //获取请求条件的种类
            String conditionType = weatherApi.getConditionType();

            List<ParamType> types = new ArrayList<>();
            for (String id : conditionType.split(",")) {
                Integer typeId = Integer.valueOf(id);
                RequestConditionEnums temp = RequestConditionEnums.getByTypeId(typeId);
                ParamType paramType = new ParamType();
                paramType.setTypeId(typeId);
                paramType.setTypeName(temp.getTypeName());
                types.add(paramType);
            }
            //获取条件

            WeatherApiVo weatherApiVo = new WeatherApiVo();
            weatherApiVo.setId(weatherApi.getId());
            weatherApiVo.setApiName(apiName);
            weatherApiVo.setRequestUrl(requestUrl);
            weatherApiVo.setTypes(types);
            weatherRequests.add(weatherApiVo);
        });

        return Result.ok(weatherRequests);
    }

    @ApiOperation("测试查询")
    @GetMapping("/sendRequest")
    public Result sendRequest(WeatherRequestQueryVo weatherRequestQueryVo){
        String jsonString = weatherApiService.sendRequest(weatherRequestQueryVo);
        return Result.ok(jsonString);
    }



}
